package com.csu.client.module.network.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import com.csu.client.logger.EIMLogger;
import com.csu.client.module.messageProcess.ClientReceivedMessageProcess;
import com.csu.client.module.network.socket.ConnectSession;
import com.csu.client.tools.EIMClientConfig;
import com.csu.message.BaseMessage;
import com.daohang361.imqq.ui.EIMLoginFrame;
import com.daohang361.imqq.ui.trayIcon.EIMTrayIcon;


/**
 * 
 * @author 胡海亮  QQ:249782944
 * 学校:中南大学 商学院
 * EnterpriseIMClient :企业即时通讯
 * May 11, 2010
 * 
 * 
 */
public class ClientChatHandler extends IoHandlerAdapter {

	/**
	 * 
	 * 当接口中其他方法抛出异常未被捕获时触发此方法 
	 */
	public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception {
		
	}

	/**
	 * 当接收到消息后被触发 
	 * 
	 */
	public void messageReceived(IoSession ioSession, Object msg) throws Exception {
		BaseMessage message=(BaseMessage)msg;
		EIMLogger.INFO("接受到得消息为："+message);
		ClientReceivedMessageProcess messageProcess=new ClientReceivedMessageProcess();
		messageProcess.processMessage(message);
	}

	/**
	 * 当发送消息后被触发 
	 * 
	 */
	public void messageSent(IoSession ioSession, Object msg) throws Exception {
		
	}

	/**
	 * 当会话关闭时被触发
	 * 
	 */
	public void sessionClosed(IoSession ioSession) throws Exception {
		EIMLogger.Error("与服务器断开连接！！");
		EIMTrayIcon trayIcon=EIMTrayIcon.getInStance();
		trayIcon.showIcon(EIMClientConfig.OffLineTryIcon_Type);
	}

	/**
	 * 当会话创建时被触发 
	 * 
	 */
	public void sessionCreated(IoSession ioSession) throws Exception {
		
	}
	/**
	 * 
	 * 当会话空闲时被触发 
	 */
	public void sessionIdle(IoSession ioSession, IdleStatus msg) throws Exception {
		
	}
	/**
	 * 
	 * 当会话开始时被触发 
	 */
	public void sessionOpened(IoSession ioSession) throws Exception {
		ConnectSession clientConnect=ConnectSession.getInstance();
		EIMLogger.INFO("会话已经打开");
		clientConnect.setTextSession(ioSession);
		clientConnect.sendTextMessage(EIMLoginFrame.getInstance().getLoginMessage());
	}



}
